﻿using System;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using cfg = System.Configuration;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
using System.Drawing;
//这里的明细来自于报关装箱单明细的分组合计，参见ShowContractSheetByContractNo
//
namespace BYManageInfoSystem
{
    public partial class Frm_SeaGateOutPutContract : DockContent
    {
        private DGBY.BLL.HxdNoTab m_bllHxdNoTab = new DGBY.BLL.HxdNoTab();
        private bool m_bHaveChange = false;
        private DataSet m_dsPaidan = new DataSet();
        private string m_strFolder = "d:/DGExcel/";
        private bool m_bFlag = false;
        private DGBY.BLL.SJGouHHTTab m_bllSJGouHHTTab = new DGBY.BLL.SJGouHHTTab();
        private DGBY.BLL.TYInvoiceTab m_bllTYInvoiceTab = new DGBY.BLL.TYInvoiceTab();
      //  private DGBY.BLL.BGDComeBackGoodsMXTab m_bllBGDComeBackGoodsMXTab = new DGBY.BLL.BGDComeBackGoodsMXTab();
        private DGBY.BLL.CargoBoxTab m_bllCargoBoxTab = new DGBY.BLL.CargoBoxTab();
        private DGBY.BLL.BGPackingTab m_bllBGPackingTab = new DGBY.BLL.BGPackingTab();
     //   private DGBY.BLL.BGDComeBackTab m_bllBGDComeBackTab = new DGBY.BLL.BGDComeBackTab();
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.T_ACL_User m_bllT_ACL_User = new DGBY.BLL.T_ACL_User();
        private DGBY.BLL.QDPlaceTab m_bllQDPlaceTab = new DGBY.BLL.QDPlaceTab();
        private DGBY.BLL.YSMethodTab m_bllYSMethodTab = new DGBY.BLL.YSMethodTab();
        private DGBY.BLL.MDCountryTab m_bllMDCountryTab = new DGBY.BLL.MDCountryTab();
        private DGBY.BLL.JNHYPosTab m_bllJNHYPosTab = new DGBY.BLL.JNHYPosTab();
        //private DGBY.BLL.BGPackingTab m_bllBGPackingTab = new DGBY.BLL.BGPackingTab();
        private DGBY.BLL.BGCKHTTab m_bllBGCKHTTab = new DGBY.BLL.BGCKHTTab();
        private DGBY.BLL.BGPackGoodsMXTab m_bllBGPackGoodsMXTab = new DGBY.BLL.BGPackGoodsMXTab();
        private DGBY.BLL.BiZhiTab m_bllBiZhiTab = new DGBY.BLL.BiZhiTab();
        private DGBY.BLL.NumDanWeiTab m_bllNumDanWeiTab = new DGBY.BLL.NumDanWeiTab();
        private DGBY.BLL.ZYHarborTab m_bllZYHarborTab = new DGBY.BLL.ZYHarborTab();
        private DGBY.BLL.SHCompTab m_bllSHCompTab = new DGBY.BLL.SHCompTab();
        private DGBY.BLL.BYCompanyTab bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.BZSortTab m_bllBZSortTab = new DGBY.BLL.BZSortTab();

        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        //ExcelBase eB = new ExcelBase();

        private int m_nSelectBGCKHTTabID = 0;
        private int m_RowIndex = 0;
        private bool CheckSBDate()
        {
            bool bRet = false;
            if (!Frm_Main.m_bCompleteVer)
            {
                DataSet dsHxdNoTab = m_bllHxdNoTab.GetList("HxdNoTab.PaidanRegisterTabID = '" + this.comboBoxContractNo.SelectedValue + "'");
                if (dsHxdNoTab.Tables.Count > 0 && dsHxdNoTab.Tables[0].Rows.Count > 0)
                {
                    int nHxdNoTabID = Convert.ToInt32(dsHxdNoTab.Tables[0].Rows[0]["HxdNoTabID"].ToString(), 10);
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nHxdNoTabID);
                    int nYear = modelHxdNoTabOld.SBDate.Value.Date.Year;
                    if (nYear != 1900)
                    {
                        bRet = true;
                    }
                }

            }

            return bRet;
        }
       // private DataSet m_ds = new DataSet();
 

        public Frm_SeaGateOutPutContract()
        {
            InitializeComponent();
        }

        private void InitCombox()
        {
            m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndID("");
            if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            }

            DataSet ds = m_bllT_ACL_User.GetList("Name != 'System1'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxSellPerson.DataSource = ds.Tables[0];
                this.comboBoxSellPerson.DisplayMember = "用户名称";
                this.comboBoxSellPerson.ValueMember = "UserPasswordTabID";
                this.comboBoxSellPerson.SelectedIndex = 0;
            }

            ds = m_bllQDPlaceTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxMakeProtocolPlace.DataSource = ds.Tables[0];
                this.comboBoxMakeProtocolPlace.DisplayMember = "签订地点";
                this.comboBoxMakeProtocolPlace.ValueMember = "QDPlaceTabID";
                this.comboBoxMakeProtocolPlace.SelectedIndex = 0;
            }

            ds = m_bllYSMethodTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxTransWay.DataSource = ds.Tables[0];
                this.comboBoxTransWay.DisplayMember = "运输方式";
                this.comboBoxTransWay.ValueMember = "YSMethodTabID";
                this.comboBoxTransWay.SelectedIndex = 0;
            }

            ds = m_bllMDCountryTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxTargetCountry.DataSource = ds.Tables[0];
                this.comboBoxTargetCountry.DisplayMember = "国家名称";
                this.comboBoxTargetCountry.ValueMember = "MDCountryTabID";
                this.comboBoxTargetCountry.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxTargetCountry.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxTargetCountry.SelectedIndex = 0;
            }

            ds = m_bllJNHYPosTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxGoodsSrcPlace.DataSource = ds.Tables[0];
                this.comboBoxGoodsSrcPlace.DisplayMember = "境内货源地";
                this.comboBoxGoodsSrcPlace.ValueMember = "JNHYPosTabID";
                this.comboBoxGoodsSrcPlace.SelectedIndex = 0;
            }


            ds = m_bllSHCompTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxRecvGoodsComp.DataSource = ds.Tables[0];
                this.comboBoxRecvGoodsComp.DisplayMember = "公司名字";
                this.comboBoxRecvGoodsComp.ValueMember = "SHCompTabID";
                this.comboBoxRecvGoodsComp.SelectedIndex = 0;
            }

            ds = m_bllZYHarborTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxPort.DataSource = ds.Tables[0];
                this.comboBoxPort.DisplayMember = "装运港名称";
                this.comboBoxPort.ValueMember = "ZYHarborTabID";
                this.comboBoxPort.SelectedIndex = 0;
            }

            ds = m_bllBZSortTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxOutSideCate.DataSource = ds.Tables[0];
                this.comboBoxOutSideCate.DisplayMember = "包装种类";
                this.comboBoxOutSideCate.ValueMember = "BZSortTabID";
                this.comboBoxOutSideCate.SelectedIndex = 0;
            }
        }

        private void Frm_SeaGateOutPutContract_Load(object sender, EventArgs e)
        {
            //this.groupBoxGoodsDetail.Visible = false;
            InitCombox();
            if (Frm_Main.m_bCompleteVer == true)
            {
                this.checkBoxDisCountBG.Visible = true;
            }
            else
            {
                this.checkBoxDisCountBG.Visible = false;
            }
  
        }
        private void SetSumFields(string strContractCode)
        {
            //this.textBoxTotalNum.Text = "";
            //this.textBoxTotalCost.Text = "";

            ////m_bllSJCKHTTab.UpdateTotalCostField(strContractCode);
            //DataSet ds = m_bllBGCKHTTab.ShowZNumZJinE("ContractCode = '" + strContractCode + "'");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    this.textBoxTotalNum.Text = ds.Tables[0].Rows[0]["ZNumber"].ToString();
            //    this.textBoxTotalCost.Text = ds.Tables[0].Rows[0]["ZCKHTJinE"].ToString();
            //}
        }

        private void ShowAllRecordInDataGridView()
        {
            DataSet ds = m_bllBGCKHTTab.ShowOneOutContractRecFromCK("");

            ultraGrid1.DataSource = null;
            ultraGrid2.DataSource = null;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndIDAll("");
            if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                //if (m_nPaidanRegisterTabID != 0)
                this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                //else
                // this.comboBoxContractNo.SelectedIndex = Frm_Main.m_nContractSelectIndex;
            }
            ShowAllRecordInDataGridView();
            this.ultraGrid1.Dock = DockStyle.Fill;
            this.groupBoxGoodsDetail.Visible = false;
        }

        private void ShowAllMXRecordInDataGridView(string strContractCode)
        {
            ultraGrid2.DataSource = null;
            DataSet m_ds = m_bllBGCKHTTab.ShowAllCKHTMX(strContractCode);
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {

                //Decimal nNumTotal = 0;
                //Decimal dtJinETotal = 0;
                //Decimal dtMaoWeight = 0;
                //Decimal dtFactWeight = 0;
                //int nNumPiece = 0;

                //for (int i = 0; i < m_ds.Tables[0].Rows.Count; i++)
                //{
                //    nNumPiece += Convert.ToInt32(m_ds.Tables[0].Rows[i]["件数"].ToString(), 10);
                //    nNumTotal += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["数量"].ToString());
                //    dtJinETotal += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["金额"].ToString());
                //    dtMaoWeight += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["毛重"].ToString());
                //    dtFactWeight += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["净重"].ToString());

                //}

                //DataRow dr = m_ds.Tables[0].NewRow();
                //dr["件数"] = nNumPiece;
                //dr["数量"] = nNumTotal;
                //dr["金额"] = dtJinETotal;
                //dr["毛重"] = dtMaoWeight;
                //dr["净重"] = dtFactWeight;
                //dr["商品名称"] = "合计";
                //m_ds.Tables[0].Rows.Add(dr.ItemArray);
                m_ds.Tables[0].Columns["打折单价"].ColumnName = "报关单价";
                m_ds.Tables[0].Columns["打折金额"].ColumnName = "报关金额";
                ultraGrid2.DataSource = m_ds.Tables[0];
                if (Frm_Main.m_bCompleteVer == false)
                {
                    ultraGrid2.DisplayLayout.Bands[0].Columns["单价"].Hidden = true;
                    ultraGrid2.DisplayLayout.Bands[0].Columns["金额"].Hidden = true;
                }
                //ultraGrid2.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            }
           // SetSumFields(strContractCode);
        }

        private void ShowContractSheetByDJNo(string strDJNo)
        {
            DataSet ds = m_bllBGCKHTTab.ShowOneOutContractRecFromSJ("BGPackDJNo = '" + strDJNo + "'");

            ultraGrid1.DataSource = null;
            ultraGrid2.DataSource = null;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                string strContractCode = ds.Tables[0].Rows[0]["合同编号"].ToString();
                ShowAllMXRecordInDataGridView(strContractCode);
            }

        }

        private void ShowContractSheetByContractNo(string strContractNo)
        {
            int nID = Convert.ToInt32(strContractNo, 10);
            DataSet ds = m_bllBGCKHTTab.ShowOneOutContractRecFromCK("BGCKHTTab.PaidanRegisterTabID = '" + nID + "'");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = null;
                ultraGrid2.DataSource = null;
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                string strContractCode = ds.Tables[0].Rows[0]["合同编号"].ToString();
                m_nSelectBGCKHTTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BGCKHTTabID"].ToString());
                this.textBoxSecondSheetNo.Text = ds.Tables[0].Rows[0]["单据号"].ToString();
                cellBGCKHTTabClick();
                // this.textBox1ContractNoGoodsDetail.Text = strContractCode;
                ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTabID = '" + nID + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    int nCompId = Convert.ToInt32(ds.Tables[0].Rows[0]["BYCompanyTabID"].ToString(), 10);
                    DataSet ds1 = m_bllBYCompanyTab.GetList("BYCompanyTabID = '" + nCompId + "'");
                    if (ds1.Tables.Count > 0 && ds1.Tables[0].Rows.Count > 0)
                    {
                        this.textBoxSellSide.Text = ds1.Tables[0].Rows[0]["经营单位名称"].ToString();
                    }
                }
                ShowAllMXRecordInDataGridView(strContractCode);
                //this.buttonAddFillBoxSheet.Enabled = true;
                this.buttonDelFillBoxSheet.Enabled = true;
                this.buttonModifyFillBoxSheet.Enabled = true;
            }
            else
            {
                m_nSelectBGCKHTTabID = 0;
                ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTabID = '" + nID + "'");

                string strContractCode = ds.Tables[0].Rows[0]["ContractCode"].ToString();
                //这里是当报关出口合同不存在的时候，找到一个报关装箱单显示，目前已经不存在这种情况了。
                DataSet ds2 = m_bllBGPackingTab.CheckMinBGPackDJNo("ContractCode = '" + strContractCode + "'");
                if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                {
                    ds2 = m_bllBGPackingTab.GetMinBGPackDJNo("ContractCode = '" + strContractCode + "'");
                    string strDJNo = ds2.Tables[0].Rows[0]["MINBGPackDJNo"].ToString();
                    ds2 = m_bllBGPackingTab.ShowOneBGPackRec("BGPackDJNo = '" + strDJNo + "'");
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        this.textBoxSellSide.Text = ds2.Tables[0].Rows[0]["经营单位"].ToString();
                        this.comboBoxRecvGoodsComp.Text = ds2.Tables[0].Rows[0]["收货单位"].ToString();
                        this.comboBoxPort.Text = ds2.Tables[0].Rows[0]["装运港"].ToString();
                        this.comboBoxOutSideCate.Text = ds2.Tables[0].Rows[0]["包装种类"].ToString();
                       // this.textBox1ContractNoGoodsDetail.Text = strContractCode;
                    }
                    ShowContractSheetByDJNo(strDJNo);
                    if (strDJNo != "")
                    {
                       // this.buttonAddFillBoxSheet.Enabled = true;
                        this.buttonDelFillBoxSheet.Enabled = true;
                        this.buttonModifyFillBoxSheet.Enabled = true;
                    }
                    else
                    {
                       // this.buttonAddFillBoxSheet.Enabled = false;
                        this.buttonDelFillBoxSheet.Enabled = false;
                        this.buttonModifyFillBoxSheet.Enabled = false;
                    }
                }
            }

            
        }

        private void comboBoxContractNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxContractNo.SelectedValue.ToString();
                ShowContractSheetByContractNo(str);

            }
            catch { }
        }

        //private void dataGridViewOutSideContractList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        //{
        //    string strBGCKHTTab = this.dataGridViewOutSideContractList.Rows[this.dataGridViewOutSideContractList.CurrentCell.RowIndex].Cells[0].Value.ToString();
        //    m_nSelectBGCKHTTabID = Convert.ToInt32(strBGCKHTTab, 10);
        //    cellBGCKHTTabClick();
        //}
        private void cellBGCKHTTabClick()
        {
            this.ultraGrid1.Dock = DockStyle.None;
            this.groupBoxGoodsDetail.Visible = true;
            DGBY.Model.BGCKHTTab modelBGCKHTTab = m_bllBGCKHTTab.GetModel(m_nSelectBGCKHTTabID);
            if (modelBGCKHTTab != null)
            {
                this.comboBoxSellPerson.Text = modelBGCKHTTab.JYCompDaiBiao;
                this.comboBoxBuyPerson.Text = modelBGCKHTTab.SHCompDaiBiao;
                this.textBoxDYZ.Text = modelBGCKHTTab.DYZ;
                this.textBoxDanBao.Text = modelBGCKHTTab.BaoXian;
                this.textBoxPayCondition.Text = modelBGCKHTTab.FKCondition;
                this.textBoxGoodsCheck.Text = modelBGCKHTTab.SPCheck;
                this.textBoxProblemSolve.Text = modelBGCKHTTab.ZYMethod;
                this.textBoxProductNo.Text = modelBGCKHTTab.SCPiNo;
                this.textBoxOther.Text = modelBGCKHTTab.QTThing;
                this.textBoxNote.Text = modelBGCKHTTab.Note;
                this.textBoxSecondSheetNo.Text = modelBGCKHTTab.BGCKHTDJNo.ToString();
                this.textBoxInvoiceNo.Text = modelBGCKHTTab.InvoiceNo;
                this.checkBoxDisCountBG.Checked = modelBGCKHTTab.isDisCount;
                this.dateTimePickerMakeProtocolDate.Value = (System.DateTime)modelBGCKHTTab.QDDate;
                this.dateTimePickerDeadLine.Value = (System.DateTime)modelBGCKHTTab.JHDate;

                DataSet ds = m_bllBGPackingTab.GetList("BGPackingTab.PaidanRegisterTabID = '" + modelBGCKHTTab.PaidanRegisterTabID.ToString() + "'");
                int nZYHarborTabID = 0;
                int nBZSortTabID = 0;
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    nBZSortTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BZSortTabID"].ToString(), 10);
                    nZYHarborTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["ZYHarborTabID"].ToString(), 10);
                }

                if (modelBGCKHTTab.QDPlaceTabID != null)
                    this.comboBoxMakeProtocolPlace.SelectedValue = modelBGCKHTTab.QDPlaceTabID;
                if (modelBGCKHTTab.YSMethodTabID != null)
                    this.comboBoxTransWay.SelectedValue = modelBGCKHTTab.YSMethodTabID;
                if (modelBGCKHTTab.MDCountryTabID != null)
                    this.comboBoxTargetCountry.SelectedValue = modelBGCKHTTab.MDCountryTabID;
                if (modelBGCKHTTab.JNHYPosTabID != null)
                    this.comboBoxGoodsSrcPlace.SelectedValue = modelBGCKHTTab.JNHYPosTabID;
                if (modelBGCKHTTab.PaidanRegisterTabID != null)
                    this.comboBoxContractNo.SelectedValue = modelBGCKHTTab.PaidanRegisterTabID;
                if (modelBGCKHTTab.ZYHarborTabID != null)
                    this.comboBoxPort.SelectedValue = nZYHarborTabID;
                if (modelBGCKHTTab.SHCompTabID != null)
                    this.comboBoxRecvGoodsComp.SelectedValue = modelBGCKHTTab.SHCompTabID;
                if (modelBGCKHTTab.BZSortTabID != null)
                    this.comboBoxOutSideCate.SelectedValue = nBZSortTabID;

                //ShowAllMXRecordInDataGridView(m_nSelectSJPackingTabID);

            }
        }


        private Int64 MakeBGPackDJNo()
        {
            string strDate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0');
            DataSet ds = m_bllBGCKHTTab.GetBGPackDJNoByDate("BGCKHTDJDate = '" + strDate + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strDJNo = ds.Tables[0].Rows[0]["MaxBGDJNum"].ToString();
                long nDJNo = 0;
                if (strDJNo != "")
                    nDJNo = Convert.ToInt64(strDJNo, 10);
                nDJNo++;
                string strTemp = "";
                if (strDJNo != "")
                    strTemp = string.Format("{0:d4}", nDJNo);
                else
                    strTemp = strDate + string.Format("{0:d4}", nDJNo);

                return Convert.ToInt64(strTemp, 10);
            }

            return 0;
        }

        private void buttonAddFillBoxSheet_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BGCKHTTab modelBGCKHTTab = new DGBY.Model.BGCKHTTab();

                modelBGCKHTTab.JYCompDaiBiao = this.comboBoxSellPerson.Text;
                modelBGCKHTTab.SHCompDaiBiao = this.comboBoxBuyPerson.Text;
                modelBGCKHTTab.DYZ = this.textBoxDYZ.Text;
                modelBGCKHTTab.BaoXian = this.textBoxDanBao.Text;
                modelBGCKHTTab.FKCondition = this.textBoxPayCondition.Text;
                modelBGCKHTTab.SPCheck = this.textBoxGoodsCheck.Text;
                modelBGCKHTTab.ZYMethod = this.textBoxProblemSolve.Text;
                modelBGCKHTTab.SCPiNo = this.textBoxProductNo.Text;
                modelBGCKHTTab.QTThing = this.textBoxOther.Text;
                modelBGCKHTTab.Note = this.textBoxNote.Text;

                modelBGCKHTTab.BGCKHTDJNo = MakeBGPackDJNo();

                //modelBGCKHTTab.QDDate = this.dateTimePickerMakeProtocolDate.Value;
                //modelBGCKHTTab.JHDate = this.dateTimePickerDeadLine.Value;
                modelBGCKHTTab.QDDate = this.dateTimePickerMakeProtocolDate.Value.Date;
                modelBGCKHTTab.JHDate = this.dateTimePickerMakeProtocolDate.Value.AddMonths(1).Date; 
                modelBGCKHTTab.BGCKHTDJDate = System.DateTime.Now.Date;

                modelBGCKHTTab.InvoiceNo = this.textBoxInvoiceNo.Text;
                modelBGCKHTTab.InvoiceNote = "";

                if (this.comboBoxMakeProtocolPlace.SelectedValue != null)
                    modelBGCKHTTab.QDPlaceTabID = Convert.ToInt32(this.comboBoxMakeProtocolPlace.SelectedValue.ToString(), 10);
                if (this.comboBoxTransWay.SelectedValue != null)
                    modelBGCKHTTab.YSMethodTabID = Convert.ToInt32(this.comboBoxTransWay.SelectedValue.ToString(), 10);
                if (this.comboBoxTargetCountry.SelectedValue != null)
                    modelBGCKHTTab.MDCountryTabID = Convert.ToInt32(this.comboBoxTargetCountry.SelectedValue.ToString(), 10);
                if (this.comboBoxGoodsSrcPlace.SelectedValue != null)
                    modelBGCKHTTab.JNHYPosTabID = Convert.ToInt32(this.comboBoxGoodsSrcPlace.SelectedValue.ToString(), 10);
                if (this.comboBoxContractNo.SelectedValue != null)
                    modelBGCKHTTab.PaidanRegisterTabID = Convert.ToInt32(this.comboBoxContractNo.SelectedValue.ToString(), 10);

                if (this.comboBoxPort.SelectedValue != null)
                    modelBGCKHTTab.ZYHarborTabID = Convert.ToInt32(this.comboBoxPort.SelectedValue.ToString(), 10);
                if (this.comboBoxRecvGoodsComp.SelectedValue != null)
                    modelBGCKHTTab.SHCompTabID = Convert.ToInt32(this.comboBoxRecvGoodsComp.SelectedValue.ToString(), 10);
                if (this.comboBoxOutSideCate.SelectedValue != null)
                    modelBGCKHTTab.BZSortTabID = Convert.ToInt32(this.comboBoxOutSideCate.SelectedValue.ToString(), 10);


                m_bllBGCKHTTab.Add(modelBGCKHTTab);
                ShowContractSheetByContractNo(modelBGCKHTTab.PaidanRegisterTabID.ToString());

                //DataSet ds2 = m_bllBGDComeBackTab.GetList("BGDComeBackTab.PaidanRegisterTabID = '" + modelBGCKHTTab.PaidanRegisterTabID + "'");
                //if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                //{
                //    return;
                //}
                ////回收报关单
                //DGBY.Model.BGDComeBackTab modelBGDComeBackTab = new DGBY.Model.BGDComeBackTab();

                //modelBGDComeBackTab.BGDNo = "";
                //modelBGDComeBackTab.FHDWName = this.textBoxSellSide.Text;
                //modelBGDComeBackTab.PZWH = "";
                //modelBGDComeBackTab.TotalPieceNum = "0";
                //modelBGDComeBackTab.TotalMaoWeight = "0";
                //modelBGDComeBackTab.TotalJingWeight = "0";
                //modelBGDComeBackTab.SFDZName = "";
                //modelBGDComeBackTab.SFDZNo = "";
                //modelBGDComeBackTab.Note = "";

                //modelBGDComeBackTab.SBDate = System.DateTime.Now.Date;
                //modelBGDComeBackTab.CKouDate = modelBGCKHTTab.QDDate;


                //modelBGDComeBackTab.PaidanRegisterTabID = modelBGCKHTTab.PaidanRegisterTabID;
                //modelBGDComeBackTab.YSMethodTabID = modelBGCKHTTab.YSMethodTabID;
                //modelBGDComeBackTab.MDCountryTabID = modelBGCKHTTab.MDCountryTabID;
                //modelBGDComeBackTab.JNHYPosTabID = modelBGCKHTTab.JNHYPosTabID;
                //modelBGDComeBackTab.ChengJFSTabID = 1;
                //modelBGDComeBackTab.MDPortTabID = 1;
                //modelBGDComeBackTab.BZSortTabID = modelBGCKHTTab.BZSortTabID;
                //modelBGDComeBackTab.BGHDataTabID = 1;
                //modelBGDComeBackTab.HaiGuanTabID = 1;
                //modelBGDComeBackTab.MaoYiFSTabID = 1;
                //modelBGDComeBackTab.JieHuiFSTabID = 1;
                //modelBGDComeBackTab.ZhengMXZTabID = 1;
                //modelBGDComeBackTab.IsSyncComplete = 1;

                //m_bllBGDComeBackTab.Add(modelBGDComeBackTab);

                //int nBGDComeBackTabID = 0;
                //DataSet ds = m_bllBGDComeBackTab.GetList("PaidanRegisterTabID = '" + modelBGDComeBackTab.PaidanRegisterTabID.ToString() + "'");
                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    nBGDComeBackTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BGDComeBackTabID"].ToString(), 10);
                //}
                //ds = m_bllBGPackingTab.GetList("PaidanRegisterTabID = '" + modelBGDComeBackTab.PaidanRegisterTabID.ToString() + "'");
                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                //    {
                //        DGBY.Model.CargoBoxTab modelCargoBoxTab = new DGBY.Model.CargoBoxTab();

                //        modelCargoBoxTab.CargoBoxNo = ds.Tables[0].Rows[0]["GuiNo"].ToString();
                //        modelCargoBoxTab.GuiXingTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["GuiXingTabID"].ToString(), 10);

                //        modelCargoBoxTab.BGDComeBackTabID = nBGDComeBackTabID;

                //        m_bllCargoBoxTab.Add(modelCargoBoxTab);
                //    }
                //}

                //string strContractCode = "";
                //ds = m_bllPaidanRegisterTab.GetAllPaiDanList("PaidanRegisterTabID = '" + modelBGDComeBackTab.PaidanRegisterTabID.ToString() + "'");
                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    strContractCode = ds.Tables[0].Rows[0]["合同编号"].ToString();
                //}
                ////这里转移到修改合同时进行处理
                //ds = m_bllBGCKHTTab.ShowAllCKHTMXEx(strContractCode);
                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                //    {
                //        DGBY.Model.BGDComeBackGoodsMXTab modelBGDComeBackGoodsMXTab = new DGBY.Model.BGDComeBackGoodsMXTab();

                //        modelBGDComeBackGoodsMXTab.GoodsNo = "";
                //        modelBGDComeBackGoodsMXTab.GoodsMXName = ds.Tables[0].Rows[i]["商品名称"].ToString();
                //        modelBGDComeBackGoodsMXTab.GGXHZDEtc = ds.Tables[0].Rows[i]["规格型号质地"].ToString();
                //        modelBGDComeBackGoodsMXTab.FPieceNum = Convert.ToInt32(ds.Tables[0].Rows[i]["数量"].ToString(), 10);
                //        modelBGDComeBackGoodsMXTab.SingleCost = Convert.ToDecimal(ds.Tables[0].Rows[i]["单价"].ToString());
                //        modelBGDComeBackGoodsMXTab.TotalCost = Convert.ToDecimal(ds.Tables[0].Rows[i]["金额"].ToString());
                //        modelBGDComeBackGoodsMXTab.ZhengMian = "";
                //        modelBGDComeBackGoodsMXTab.BGDComeBackTabID = nBGDComeBackTabID;

                //      //  modelBGDComeBackGoodsMXTab.BiZhiTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["币制"].ToString(), 10);
                //        modelBGDComeBackGoodsMXTab.NumDanWeiTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["单位"].ToString(), 10);
                //        modelBGDComeBackGoodsMXTab.BGSeqNo = Convert.ToInt32(ds.Tables[0].Rows[i]["序号"].ToString(), 10);
                //        m_bllBGDComeBackGoodsMXTab.Add(modelBGDComeBackGoodsMXTab);
                //    }
                //}

                //进出口统一发票
                DGBY.Model.TYInvoiceTab modelTYInvoiceTab = new DGBY.Model.TYInvoiceTab();
                modelTYInvoiceTab.PaidanRegisterTabID = modelBGCKHTTab.PaidanRegisterTabID;
                modelTYInvoiceTab.ZhuanYPort = "";
                modelTYInvoiceTab.JieHuiFSTabID = 1;
                modelTYInvoiceTab.MaoYiFSTabID = 1;
                modelTYInvoiceTab.Note = "";
                modelTYInvoiceTab.LCNo = "";
                modelTYInvoiceTab.KPDate = System.DateTime.Now;
                modelTYInvoiceTab.BYCompBankCountTabID = 1;
                modelTYInvoiceTab.IsPrint = 0;
                modelTYInvoiceTab.YSMethodTabID = modelBGCKHTTab.YSMethodTabID;
                m_bllTYInvoiceTab.Add(modelTYInvoiceTab);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonDelFillBoxSheet_Click(object sender, EventArgs e)
        {
            if (CheckSBDate())
            {
                MessageBox.Show("综合登记表该合同号已经登记申报日期，不能修改单据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DGBY.Model.BGCKHTTab modelBGCKHTTabOld = m_bllBGCKHTTab.GetModel(m_nSelectBGCKHTTabID);
            if (modelBGCKHTTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllBGCKHTTab.Delete(m_nSelectBGCKHTTabID);
                //DataSet ds = m_bllBGDComeBackTab.ShowOneBGDComeBackkRec("PaidanRegisterTab.PaidanRegisterTabID = '" + modelBGCKHTTabOld.PaidanRegisterTabID.ToString() + "'");

                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    int nIsSyncComplete = Convert.ToInt32(ds.Tables[0].Rows[0]["同步已完成"].ToString(), 10);
                //    if (nIsSyncComplete == 0)
                //    {
                //        int nBGDComeBackTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BGDComeBackTabID"].ToString(), 10);

                //        m_bllBGDComeBackTab.Delete(nBGDComeBackTabID);

                //    }
                //}
                Frm_Main.Log2DB(this.Text, "删除", this.comboBoxContractNo.Text);
                ShowContractSheetByContractNo(modelBGCKHTTabOld.PaidanRegisterTabID.ToString());
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.textBoxDYZ.Text = "";
                this.textBoxProductNo.Text = "";
                this.textBoxNote.Text = "";

                return;
            }
        }

        private void buttonModifyFillBoxSheet_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckSBDate())
                {
                    MessageBox.Show("综合登记表该合同号已经登记申报日期，不能修改单据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.BGCKHTTab modelBGCKHTTabOld = m_bllBGCKHTTab.GetModel(m_nSelectBGCKHTTabID);
                if (modelBGCKHTTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //DGBY.Model.BGCKHTTab modelBGCKHTTab = new DGBY.Model.BGCKHTTab();
                //modelBGCKHTTab.BGCKHTTabID = m_nSelectBGCKHTTabID;
                //modelBGCKHTTabOld.BGCKHTDJNo = modelBGCKHTTabOld.BGCKHTDJNo;

                modelBGCKHTTabOld.JYCompDaiBiao = this.comboBoxSellPerson.Text;
                modelBGCKHTTabOld.SHCompDaiBiao = this.comboBoxBuyPerson.Text;
                modelBGCKHTTabOld.DYZ = this.textBoxDYZ.Text;
                modelBGCKHTTabOld.BaoXian = this.textBoxDanBao.Text;
                modelBGCKHTTabOld.FKCondition = this.textBoxPayCondition.Text;
                modelBGCKHTTabOld.SPCheck = this.textBoxGoodsCheck.Text;
                modelBGCKHTTabOld.ZYMethod = this.textBoxProblemSolve.Text;
                modelBGCKHTTabOld.SCPiNo = this.textBoxProductNo.Text;
                modelBGCKHTTabOld.QTThing = this.textBoxOther.Text;
                modelBGCKHTTabOld.Note = this.textBoxNote.Text;
                if (modelBGCKHTTabOld.isDisCount != this.checkBoxDisCountBG.Checked)
                {

                    ModifyContractGoodsDetails();

                }
                modelBGCKHTTabOld.isDisCount = this.checkBoxDisCountBG.Checked;

                modelBGCKHTTabOld.QDDate = this.dateTimePickerMakeProtocolDate.Value.Date;
                modelBGCKHTTabOld.JHDate = this.dateTimePickerMakeProtocolDate.Value.AddMonths(1).Date;
               // modelBGCKHTTabOld.BGCKHTDJDate = modelBGCKHTTabOld.BGCKHTDJDate;
                modelBGCKHTTabOld.InvoiceNo = this.textBoxInvoiceNo.Text;
                modelBGCKHTTabOld.InvoiceNote = "";

                if (this.comboBoxMakeProtocolPlace.SelectedValue != null)
                    modelBGCKHTTabOld.QDPlaceTabID = Convert.ToInt32(this.comboBoxMakeProtocolPlace.SelectedValue.ToString(), 10);
                if (this.comboBoxTransWay.SelectedValue != null)
                    modelBGCKHTTabOld.YSMethodTabID = Convert.ToInt32(this.comboBoxTransWay.SelectedValue.ToString(), 10);
                if (this.comboBoxTargetCountry.SelectedValue != null)
                    modelBGCKHTTabOld.MDCountryTabID = Convert.ToInt32(this.comboBoxTargetCountry.SelectedValue.ToString(), 10);
                if (this.comboBoxGoodsSrcPlace.SelectedValue != null)
                    modelBGCKHTTabOld.JNHYPosTabID = Convert.ToInt32(this.comboBoxGoodsSrcPlace.SelectedValue.ToString(), 10);
               // if (this.comboBoxContractNo.SelectedValue != null)
               // modelBGCKHTTabOld.PaidanRegisterTabID = modelBGCKHTTabOld.PaidanRegisterTabID;


                if (this.comboBoxPort.SelectedValue != null)
                    modelBGCKHTTabOld.ZYHarborTabID = Convert.ToInt32(this.comboBoxPort.SelectedValue.ToString(), 10);
                if (this.comboBoxRecvGoodsComp.SelectedValue != null)
                    modelBGCKHTTabOld.SHCompTabID = Convert.ToInt32(this.comboBoxRecvGoodsComp.SelectedValue.ToString(), 10);
                if (this.comboBoxOutSideCate.SelectedValue != null)
                    modelBGCKHTTabOld.BZSortTabID = Convert.ToInt32(this.comboBoxOutSideCate.SelectedValue.ToString(), 10);

               // modelBGCKHTTab.ZYHarborTabID = modelBGCKHTTabOld.ZYHarborTabID;
                m_bllBGCKHTTab.Update(modelBGCKHTTabOld);

                Frm_Main.Log2DB(this.Text, "修改", this.comboBoxContractNo.Text);
                ShowContractSheetByContractNo(modelBGCKHTTabOld.PaidanRegisterTabID.ToString());


                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        
        }





        //private void buttonGoodsDetailModify_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        m_bllBGCKHTTab.OnBtnUpdateMX(this.textBoxSingleCost.Text, this.textBoxFOB.Text, this.textBox1ContractNoGoodsDetail.Text
        //                                , this.textBoxGoodsName.Text, this.textBoxMerial.Text, this.textBoxNum.Text, this.textBoxUnit.Text
        //                                , this.textBoxCoinUnit.Text);
        //        ShowAllMXRecordInDataGridView(this.textBox1ContractNoGoodsDetail.Text);
        //        ShowGoodsDetail(m_RowIndex);
        //        MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //    }
        //    catch (System.Exception ex)
        //    {
        //        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        //    }
            
        //}

        private void buttonCopySheet_Click(object sender, EventArgs e)
        {
            DataSet ds = m_bllBGCKHTTab.ShowOneOutContractRecByContractCode(this.comboBoxContractNo.Text);

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                int nBGCKHTTabIDCount = Convert.ToInt32(ds.Tables[0].Rows[0]["BGCKHTTabIDCount"].ToString(), 10);
                if(nBGCKHTTabIDCount > 0)
                {
                    MessageBox.Show("该合同编号已有对应的报关_出口合同了，因此无法复制单据，请重新选择合同编号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

            }

            ds = m_bllBGCKHTTab.ShowOneOutContractRecFromCK("BGCKHTTab.BGCKHTDJNo = '" + this.textBoxSecondSheetNo.Text + "'");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ds = m_bllBGPackingTab.ShowOneBGPackRec("ContractCode = '" + this.comboBoxContractNo.Text + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    int nPaidanRegisterTabID = 0;
                    DateTime dt = System.DateTime.Now.Date;
                    Int64 n64NewDJNo = MakeBGPackDJNo();
                    DataSet dsTemp = m_bllPaidanRegisterTab.GetAllPaiDanList("ContractCode = '" + this.comboBoxContractNo.Text + "'");
                    if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                    {
                        nPaidanRegisterTabID = Convert.ToInt32(dsTemp.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
                        m_bllBGCKHTTab.CopyDJNo(nPaidanRegisterTabID, dt, n64NewDJNo, Convert.ToInt64(this.textBoxSecondSheetNo.Text, 10));
                        ShowContractSheetByContractNo(nPaidanRegisterTabID.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("该单据号没有对应的商检_装箱单记录，因此无法复制单据，请重新输入单据号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("该单据号没有对应的商检_出口合同记录，因此无法复制单据，请重新输入单据号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private bool IsNum(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[1-9]+\d*$");
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }
        private void GDContract2Excel(ArrayList lstExcelFileName)
        {
            DataTable dt = null;
            string str = this.comboBoxContractNo.SelectedValue.ToString();
            int nID = Convert.ToInt32(str, 10);
            DataSet m_ds = m_bllTYInvoiceTab.ShowTYInvoiceMXRec(this.comboBoxContractNo.Text);
            string strBiZhiEng = "";
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = (DataTable)m_ds.Tables[0];
               // if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("销售单价");
                    dt.Columns.Remove("销售总额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
 
                string strBiZhi = m_ds.Tables[0].Rows[0]["币种"].ToString();
                DataSet dsTemp = m_bllBiZhiTab.GetList("BiZhiTab.BiZhiName = '" + strBiZhi + "'");
                if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                {
                    strBiZhiEng = dsTemp.Tables[0].Rows[0]["币制简写"].ToString();
                }
            }

            DataSet ds = m_bllTYInvoiceTab.ShowTYInvoiceRec("TYInvoiceTab.PaidanRegisterTabID = '" + nID + "'");
            DateTime dtTemp = (DateTime)ds.Tables[0].Rows[0]["开票日期"];
            DataTable dt1 = ds.Tables[0];
            if (dt1 != null)
            {
                //if (dt1.Rows.Count > 1)
                //{
                //    MessageBox.Show("请选择不多于一条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}
            }
            else
            {
                MessageBox.Show("记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {


                    string outFile = m_strFolder + "广东省发票" + ".xls";
                    string templetFile = templetFilePath + "广东省出口商品统一发票.xlt";
                    lstExcelFileName.Add(outFile);
       

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.GDTYInvoice_DataTableToExcel(dt, dt1, 20, 14, 3, "页", 2, strBiZhiEng, "", dtTemp);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void SJGouContract2Excel(ArrayList lstExcelFileName)
        {
            if (Directory.Exists(m_strFolder))//判断是否存在
            {
                //if (File.Exists(strFolder + @"\" + strFin + ".jpg"))
                //    return;

            }
            else
            {
                //Response.Write("不存在，正在创建");
                Directory.CreateDirectory(m_strFolder);//创建新路径
            }

            DataSet ds = m_bllSJGouHHTTab.ShowAllGouHHTMXBG("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            DataTable dt = ds.Tables[0];

         //   if (Frm_Main.m_bCompleteVer == true)
            {
                dt.Columns.Remove("单价");
                dt.Columns.Remove("金额");
                //dt.Columns["打折单价"].ColumnName = "单价";
                //dt.Columns["打折金额"].ColumnName = "金额";
            }
            //else
            //{
            //    dt.Columns.Remove("打折单价");
            //    dt.Columns.Remove("打折金额");
            //}
            DataSet dsTemp = m_bllSJGouHHTTab.ShowOneBuyGoodsRecEx("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            DataTable dt1 = dsTemp.Tables[0];
            //DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            //DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条购货合同记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("购货合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {

                    string outFile = m_strFolder + "购货合同" + ".xls";
                    string templetFile = templetFilePath + "商检_购货合同草稿.xlt";
                    lstExcelFileName.Add(outFile);

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.CommerCheckBuyGoodsConstract_DataTableToExcel(dt, dt1, 20, 8, 1, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("购货合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Contract2Excel(ArrayList lstExcelFileName, int nRows)
        {
            if (Directory.Exists(m_strFolder))//判断是否存在
            {
                //if (File.Exists(strFolder + @"\" + strFin + ".jpg"))
                //    return;

            }
            else
            {
                //Response.Write("不存在，正在创建");
                Directory.CreateDirectory(m_strFolder);//创建新路径
            }
            DataTable dt = null;
            DataSet m_ds = m_bllBGCKHTTab.ShowAllCKHTMX(this.comboBoxContractNo.Text);
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = m_ds.Tables[0];
              //  if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");
            string strBizhi = dt.Rows[0]["币制"].ToString();
            m_ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = m_ds.Tables[0];

            string strPort = dt1.Rows[0]["装运口岸"].ToString();
            m_ds = m_bllZYHarborTab.GetList("ZYHarborName = '" + strPort + "'");
            DataTable dtPort = m_ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条出口合同记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("出口合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string outFile = m_strFolder + "出口合同" + ".xls";

                    string templetFile = templetFilePath + "报关_出口合同.xlt";
                    lstExcelFileName.Add(outFile);
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.SeaGateConstract_DataTableToExcel(dt, dt1, DsBYCompInfo.Tables[0]
                            , DsSHCompInfo.Tables[0], nRows, 16, 1, "页", 1, dtBizhi, dtPort);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("出口合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            //if (ClassInputValid.IsValidSplitLine(this.errorProvider1, this.textBoxSplit))
            //    return;
            //int nRows = Convert.ToInt32(this.textBoxSplit.Text, 10);
            //ArrayList lstExcelFileName = new ArrayList();
            //Contract2Excel(lstExcelFileName,nRows);
        }

        private void buttonPrintPreview_Click(object sender, EventArgs e)
        {
            if (!CheckBGPackingSheetGuiXing())
            {
                MessageBox.Show("该合同号装箱单柜型未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
              
            if (!CheckBGPackingSheetBGH())
            {
                MessageBox.Show("该合同号装箱单报关行未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = null;
            DataSet m_ds = m_bllBGCKHTTab.ShowAllCKHTMX(this.comboBoxContractNo.Text);
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = m_ds.Tables[0];
             //   if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");
            string strBizhi = dt.Rows[0]["币制"].ToString();
            m_ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = m_ds.Tables[0];

            string strPort = dt1.Rows[0]["装运口岸"].ToString();
            m_ds = m_bllZYHarborTab.GetList("ZYHarborName = '" + strPort + "'");
            DataTable dtPort = m_ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条出口合同记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("出口合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "报关_出口合同.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.SeaGateConstract_DataTableToExcelAndPrintPreview(dt, dt1
                            , DsBYCompInfo.Tables[0], DsSHCompInfo.Tables[0], 20, 16, 1, "页", 1, dtBizhi, dtPort);
                    }
                    catch (System.Exception ex)
                    {

                    }


                }
            }
            else
            {
                MessageBox.Show("出口合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            if (!CheckBGPackingSheetGuiXing())
            {
                MessageBox.Show("该合同号装箱单柜型未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!CheckBGPackingSheetBGH())
            {
                MessageBox.Show("该合同号装箱单报关行未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = null;
            DataSet m_ds = m_bllBGCKHTTab.ShowAllCKHTMX(this.comboBoxContractNo.Text);
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = m_ds.Tables[0];
              //  if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");
            string strBizhi = dt.Rows[0]["币制"].ToString();
            m_ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = m_ds.Tables[0];
            string strPort = dt1.Rows[0]["装运口岸"].ToString();
            m_ds = m_bllZYHarborTab.GetList("ZYHarborName = '" + strPort + "'");
            DataTable dtPort = m_ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条出口合同记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("出口合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "报关_出口合同.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.SeaGateConstract_DataTableToExcelAndPrint(dt, dt1, DsBYCompInfo.Tables[0]
                            , DsSHCompInfo.Tables[0], 19, 16, 1, "页", 1, dtBizhi, dtPort);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("出口合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Frm_SeaGateOutPutContract_FormClosing(object sender, FormClosingEventArgs e)
        {
            //eB.Close();
        }



        private void dateTimePickerMakeProtocolDate_ValueChanged(object sender, EventArgs e)
        {
            this.dateTimePickerDeadLine.Value = this.dateTimePickerMakeProtocolDate.Value.AddDays(29);
        }

        private void ultraGrid1_InitializeLayout_1(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);

            InitColumnWidth1();
        }

        private void InitColumnWidth1()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["单据号"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同编号"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["卖方"].Width = 160;
            ultraGrid1.DisplayLayout.Bands[0].Columns["卖方代表人"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["买方"].Width = 130;
            ultraGrid1.DisplayLayout.Bands[0].Columns["买方代表人"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["签订日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["签订地点"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["境内货源地"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["交货期限"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["包装"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["短溢装"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["运输方式"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["装运口岸"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["目的国家"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["保险"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["付款条件"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["商品检验"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["商品检验"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["争议解决"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["生产批号"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["其它事项"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["发票号码"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否打折"].Width = 40;
        }

        private void ultraGrid1_AfterCellActivate_1(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strBGCKHTTab = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectBGCKHTTabID = Convert.ToInt32(strBGCKHTTab, 10);
            cellBGCKHTTabClick();
        }

        private void ultraGrid2_AfterCellActivate_1(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid2.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strTemp = this.ultraGrid2.Rows[this.ultraGrid2.ActiveCell.Row.Index].Cells[1].Text;
            if (strTemp == "合计")
                return;
            //string strSJPackGoodsMXTabID = this.ultraGrid2.Rows[this.ultraGrid2.ActiveCell.Row.Index].Cells[0].Text;
            m_RowIndex = this.ultraGrid2.ActiveCell.Row.Index;
            ShowGoodsDetail(m_RowIndex);
        }

        private void ShowGoodsDetail(int RowIndex)
        {
            //int i = 1;
            //this.textBoxGoodsName.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            //this.textBoxMerial.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            //this.textBoxGuiGe.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            //this.textBoxNum.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            //this.textBoxUnit.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            //string strTempSingleCost = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            //string strTempFOB = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;

            //this.textBoxFOB.Text = strTempFOB;
            //this.textBoxSingleCost.Text = strTempSingleCost;
            //this.textBoxCoinUnit.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
        }
        private void ultraGrid2_InitializeLayout_1(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
            foreach (UltraGridColumn column2 in ultraGrid2.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str != "金额" && str != "单价" && str != "报关单价" && str != "报关金额")
                {
                    column2.CellAppearance.BackColor = Color.Gray;
                    column2.CellActivation = Activation.ActivateOnly;
                }
            }
            //e.Layout.Bands[0].Columns["是否合并"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
            List<string> lstTemp = new List<string>();
            lstTemp.Add("件数");
            lstTemp.Add("数量");
            lstTemp.Add("毛重");
            lstTemp.Add("净重");
            lstTemp.Add("金额");
           // if (Frm_Main.m_bCompleteVer == true)
            {
                lstTemp.Add("报关金额");
            }

            ClassUltraGridCommonSet.SetSumColumn(e, lstTemp);

            InitColumnWidth2();
        }

        private void InitColumnWidth2()
        {
            ultraGrid2.DisplayLayout.Bands[0].Columns["序号"].Width = 30;
            ultraGrid2.DisplayLayout.Bands[0].Columns["商品名称"].Width = 120;
            ultraGrid2.DisplayLayout.Bands[0].Columns["质地"].Width = 120;
            ultraGrid2.DisplayLayout.Bands[0].Columns["规格型号"].Width = 100;
            ultraGrid2.DisplayLayout.Bands[0].Columns["数量"].Width = 70;
            ultraGrid2.DisplayLayout.Bands[0].Columns["单位"].Width = 40;
            ultraGrid2.DisplayLayout.Bands[0].Columns["单价"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["金额"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["报关单价"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["报关金额"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["币制"].Width = 40;
            ultraGrid2.DisplayLayout.Bands[0].Columns["件数"].Width = 40;
            ultraGrid2.DisplayLayout.Bands[0].Columns["毛重"].Width = 70;
            ultraGrid2.DisplayLayout.Bands[0].Columns["净重"].Width = 70;
        }

        private void Invoice2Excel(ArrayList lstExcelFileName)
        {
            if (Directory.Exists(m_strFolder))//判断是否存在
            {
                //if (File.Exists(strFolder + @"\" + strFin + ".jpg"))
                //    return;

            }
            else
            {
                //Response.Write("不存在，正在创建");
                Directory.CreateDirectory(m_strFolder);//创建新路径
            }
            DataTable dt = null;
            DataSet m_ds = m_bllBGCKHTTab.ShowAllCKHTMX(this.comboBoxContractNo.Text);
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = m_ds.Tables[0];
              //  if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            string strCompanyName = dt1.Rows[0]["卖方"].ToString();
            DataSet ds = bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
            DataTable dtComp = ds.Tables[0];
            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条发票记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("发票记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    //SaveFileDialog sfd = new SaveFileDialog();
                    //sfd.Filter = "Excel文件(*.xls)|*.xls";
                    //string outFile = "";

                    //if (sfd.ShowDialog() == DialogResult.OK)
                    //{
                    //    outFile = sfd.FileName;
                    //}
                    //else
                    //{
                    //    return;
                    //}
                    string outFile = m_strFolder + "发票" + ".xls";
                    string templetFile = templetFilePath + "商检报关_发票.xlt";
                    lstExcelFileName.Add(outFile);
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.Invoice_DataTableToExcel(dt, dt1, 20, 15, 1, "页", 1, dtComp, dtBizhi);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("货物明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void buttonInvoiceExport_Click(object sender, EventArgs e)
        {
           // Invoice2Excel();
        }

        private void buttonInvoicePrintPreview_Click(object sender, EventArgs e)
        {
            if (!CheckBGPackingSheetGuiXing())
            {
                MessageBox.Show("该合同号装箱单柜型未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!CheckBGPackingSheetBGH())
            {
                MessageBox.Show("该合同号装箱单报关行未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = null;
            DataSet m_ds = m_bllBGCKHTTab.ShowAllCKHTMX(this.comboBoxContractNo.Text);
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = m_ds.Tables[0];
               // if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            string strCompanyName = dt1.Rows[0]["卖方"].ToString();
            DataSet ds = bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
            DataTable dtComp = ds.Tables[0];
            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条发票记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("发票记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "商检报关_发票.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.Invoice_DataTableToExcelAndPrintPreview(dt, dt1, 20, 15, 1, "页", 1, dtComp, dtBizhi);
                    }
                    catch (System.Exception ex)
                    {

                    }


                }
            }
            else
            {
                MessageBox.Show("货物明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonInvoicePrint_Click(object sender, EventArgs e)
        {
            if (!CheckBGPackingSheetGuiXing())
            {
                MessageBox.Show("该合同号装箱单柜型未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!CheckBGPackingSheetBGH())
            {
                MessageBox.Show("该合同号装箱单报关行未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = null;
            DataSet m_ds = m_bllBGCKHTTab.ShowAllCKHTMX(this.comboBoxContractNo.Text);
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = m_ds.Tables[0];
                //if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            string strCompanyName = dt1.Rows[0]["卖方"].ToString();
            DataSet ds = bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
            DataTable dtComp = ds.Tables[0];
            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条发票记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("发票记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "商检报关_发票.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.Invoice_DataTableToExcelAndPrint(dt, dt1, 20, 15, 1, "页", 1, dtComp, dtBizhi);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("货物明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Frm_SeaGateOutPutContract_Activated(object sender, EventArgs e)
        {
            InitContractNoCombox();
            SyncContractNo();
            //m_nDelIndex = -1;

        }
        private void InitContractNoCombox()
        {
            //DataSet ds = m_bllPaidanRegisterTab.GetAllPaiDanList("");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    this.comboBoxContractNo.DataSource = ds.Tables[0];
            //    this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
            //    this.comboBoxContractNo.DisplayMember = "合同编号";
            //    this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
            //    this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //    this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            //}
            try
            {
                if (Frm_Main.m_nContractSelectValue == 127)
                    return;
                DataRow[] drs = m_dsPaidan.Tables[0].Select("PaidanRegisterTabID = '" + Frm_Main.m_nContractSelectValue.ToString() + "'");
                if (drs.Length > 0)
                    this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                else
                {
                    m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndIDAll("");
                    if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
                    {
                        this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                        this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                        this.comboBoxContractNo.DisplayMember = "合同编号";
                        this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                        this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                        this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                    }

                    this.comboBoxRecvGoodsComp.SelectedIndex = 0;

                    this.comboBoxSellPerson.SelectedIndex = 0;

                    this.comboBoxPort.SelectedIndex = 0;
                    this.comboBoxMakeProtocolPlace.SelectedIndex = 0;
                    this.comboBoxOutSideCate.SelectedIndex = 0;

                    this.comboBoxTransWay.SelectedIndex = 0;
                    this.comboBoxTargetCountry.SelectedIndex = 0;
                    this.comboBoxGoodsSrcPlace.SelectedIndex = 0;
                }
            }
            catch (System.Exception ex)
            {
                //  MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        private void SyncContractNo()
        {
            try
            {

                // string str = this.comboBoxContractNo.SelectedValue.ToString();

                int nID = Frm_Main.m_nContractSelectValue;
                //m_nPaidanRegisterTabID = nID;
                DataSet ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTabID = '" + nID + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    int nCompId = Convert.ToInt32(ds.Tables[0].Rows[0]["BYCompanyTabID"].ToString(), 10);
                    DataSet ds1 = m_bllBYCompanyTab.GetList("BYCompanyTabID = '" + nCompId + "'");
                    if (ds1.Tables.Count > 0 && ds1.Tables[0].Rows.Count > 0)
                    {
                        //this.textBoxCompany.Text = ds1.Tables[0].Rows[0]["经营单位名称"].ToString();
                    }
                }

                string strContractCode = ds.Tables[0].Rows[0]["ContractCode"].ToString();
                ShowContractSheetByContractNo(nID.ToString());

                //this.textBoxMark.Text = "";
                //this.textBoxNote.Text = "";


            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
        }

        private void Frm_SeaGateOutPutContract_Deactivate(object sender, EventArgs e)
        {
            try
            {
                Frm_Main.m_nContractSelectValue = (int)this.comboBoxContractNo.SelectedValue;

            }
            catch (System.Exception ex)
            {
                //  MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void textBoxSingleCost_TextChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    Frm_Main.WriteLog("textBoxSingleCost_TextChanged_1BG!");
            //    if (this.textBoxNum.Text != "" && this.textBoxSingleCost.Text != "" && !m_bFlag)
            //    {
            //        Frm_Main.WriteLog("textBoxSingleCost_TextChanged_1!222");
            //        m_bFlag = true;
            //        Decimal nFPieceNum = Convert.ToDecimal(this.textBoxNum.Text);
            //        Decimal dTotalCost = Convert.ToDecimal(this.textBoxSingleCost.Text) * (Decimal)nFPieceNum;
            //        this.textBoxFOB.Text = dTotalCost.ToString();
            //        m_bFlag = false;
            //    }
            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}
        }

        private void textBoxFOB_TextChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    Frm_Main.WriteLog("textBoxFOB_TextChanged_1BG!");
            //    if (this.textBoxNum.Text != "" && this.textBoxFOB.Text != "" && !m_bFlag)
            //    {
            //        Frm_Main.WriteLog("textBoxFOB_TextChanged_1BG222!");
            //        m_bFlag = true;
            //        Decimal nFPieceNum = Convert.ToDecimal(this.textBoxNum.Text);
            //        if (nFPieceNum > 0)
            //        {
            //            Decimal dSingleCost = Convert.ToDecimal(this.textBoxFOB.Text) / (Decimal)nFPieceNum;
            //            this.textBoxSingleCost.Text = dSingleCost.ToString();
            //        }
            //        m_bFlag = false;

            //    }
            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
           

        }

        private void ZXD2Excel(ArrayList lstExcelFileName, int nRows)
        {
            DataSet dsTemp = m_bllBGCKHTTab.GetAllBGPackingID(this.comboBoxContractNo.Text);
            if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsTemp.Tables[0].Rows.Count; i++)
                {
                    int m_nSelectBGPackingTabID = Convert.ToInt32(dsTemp.Tables[0].Rows[i]["BGPackingTabID"].ToString(), 10);
                    if (!Directory.Exists(m_strFolder))//判断是否存在
                    {
                        Directory.CreateDirectory(m_strFolder);//创建新路径
                    }
                    DataSet ds = m_bllBGPackGoodsMXTab.ShowGoodsMX("BGPackGoodsMXTab.BGPackingTabID = '" + m_nSelectBGPackingTabID + "'");
                    DataTable dt = ds.Tables[0];
                //    if (Frm_Main.m_bCompleteVer == true)
                    {
                        dt.Columns.Remove("单价");
                        dt.Columns.Remove("金额");
                        dt.Columns["打折单价"].ColumnName = "单价";
                        dt.Columns["打折金额"].ColumnName = "金额";
                    }
                    //else
                    //{
                    //    dt.Columns.Remove("打折单价");
                    //    dt.Columns.Remove("打折金额");
                    //}
                    DataSet m_ds = m_bllBGPackingTab.ShowOneBGPackRec("BGPackingTabID = '" + m_nSelectBGPackingTabID + "'");
                    DataTable dt1 = m_ds.Tables[0];


                    string strCompanyName = dt1.Rows[0]["经营单位"].ToString();
                    ds = m_bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
                    DataTable dtComp = ds.Tables[0];
                    //DataTable dt = (DataTable)dataGridViewGoodsDetailList.DataSource;
                    //DataSet ds = m_bllBGPackingTab.ShowOneBGPackRec("BGPackingTab.BGPackDJNo = '" + this.textBoxSecondSheetNo.Text + "'");
                    //DataTable dt1 = ds.Tables[0];
                    if (dt1 != null)
                    {
                        if (dt1.Rows.Count > 1)
                        {
                            MessageBox.Show("请选择不多于一条装箱单记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    else
                    {
                        MessageBox.Show("装箱单记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    if (dt != null)
                    {
                        if (dt.Rows.Count > 0)
                        {
                            //SaveFileDialog sfd = new SaveFileDialog();
                            //sfd.Filter = "Excel文件(*.xls)|*.xls";
                            //string outFile = "";

                            //if (sfd.ShowDialog() == DialogResult.OK)
                            //{
                            //    outFile = sfd.FileName;
                            //}
                            //else
                            //{
                            //    return;
                            //}
                            string outFile = m_strFolder + "装箱单_" + i.ToString() + ".xls";
                            string templetFile = templetFilePath + "报关_装箱单.xlt";
                            lstExcelFileName.Add(outFile);
                            ExcelBase eB = new ExcelBase(templetFile, outFile);

                            try
                            {
                                eB.SeaGateFillBox_DataTableToExcel(dt, dt1, nRows, 12, 1, "页", 4, dtComp);
                            }
                            catch (System.Exception ex)
                            {

                            }

                            eB.Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show("装箱单明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }
        private bool CheckBGPackingSheetGuiXing()
        {
            DataSet ds = m_bllBGPackingTab.GetList("PaidanRegisterTabID = '" + this.comboBoxContractNo.SelectedValue + "'"
                + " and GuiXingTabID = 6");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private bool CheckBGPackingSheetBGH()
        {
            DataSet ds = m_bllBGPackingTab.GetList("PaidanRegisterTabID = '" + this.comboBoxContractNo.SelectedValue + "'"
                + " and BGHDataTabID = 33");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        private void buttonTogether_Click(object sender, EventArgs e)
        {
            if (!CheckBGPackingSheetGuiXing())
            {
                MessageBox.Show("该合同号装箱单柜型未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!CheckBGPackingSheetBGH())
            {
                MessageBox.Show("该合同号装箱单报关行未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (ClassInputValid.IsValidSplitLine(this.errorProvider1, this.textBoxSplit))
                return;
            int nRows = Convert.ToInt32(this.textBoxSplit.Text, 10);
            ArrayList lstExcelFileName = new ArrayList();
            if (this.comboBoxPort.Text == "东莞")
            {
                GDContract2Excel(lstExcelFileName);
                SJGouContract2Excel(lstExcelFileName);
            }
            Invoice2Excel(lstExcelFileName);
            
            ZXD2Excel(lstExcelFileName, nRows);
            Contract2Excel(lstExcelFileName, nRows);
            
            ExcelBase eB = new ExcelBase();
            string outFile = m_strFolder + this.comboBoxContractNo.Text + ".xls";
            eB.Books2Sheets(outFile, lstExcelFileName,false);
            eB.CloseAll();

        }
        private void ModifyContractGoodsDetails()
        {
    
            DataSet ds = m_bllBGCKHTTab.GetAllBGPackingID(this.comboBoxContractNo.Text);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int nPackingId = Convert.ToInt32(ds.Tables[0].Rows[i]["BGPackingTabID"].ToString(), 10);
                    string strWhere = "";
                    DataSet ds2 = m_bllBGCKHTTab.FindBGPackGoodsMXTabID(strWhere, nPackingId.ToString());
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {
                            int nSJPackGoodsMXTabID = Convert.ToInt32(ds2.Tables[0].Rows[j]["BGPackGoodsMXTabID"].ToString(), 10);
                            Decimal nNum = Convert.ToDecimal(ds2.Tables[0].Rows[j]["FNumber"].ToString());
                            DGBY.Model.BGPackGoodsMXTab modelSJPackGoodsMXTabOld = m_bllBGPackGoodsMXTab.GetModel(nSJPackGoodsMXTabID);

                            if (checkBoxDisCountBG.Checked)
                            {
                                modelSJPackGoodsMXTabOld.HMaoWeight = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.CKHTDCost * Frm_Main.m_dtDiscountFactor, 2);
                                modelSJPackGoodsMXTabOld.HZMaoWeight = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.HMaoWeight * nNum,2);
                            }
                            else
                            {
                                modelSJPackGoodsMXTabOld.HMaoWeight = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.CKHTDCost,2);
                                modelSJPackGoodsMXTabOld.HZMaoWeight = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.CKHTJinE,2);
                            }

                            m_bllBGPackGoodsMXTab.Update(modelSJPackGoodsMXTabOld);
                        }
                    }
                }
            }
        }
        private void ModifyGoodsDetails(int nIndex)
        {
            if (CheckSBDate())
            {
                MessageBox.Show("综合登记表该合同号已经登记申报日期，不能修改单据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            UltraGridRow CurrentUGR;
            CurrentUGR = ultraGrid2.Rows[nIndex];
            Decimal dtDisPrice = 0;
            if (Frm_Main.m_bCompleteVer == true)
            {
                dtDisPrice = Convert.ToDecimal(CurrentUGR.Cells["报关单价"].Value.ToString());
            }

            int nDanweiId = 0;
            DataSet ds = m_bllNumDanWeiTab.GetList("NumDanWeiName = '" + CurrentUGR.Cells["单位"].Value.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                nDanweiId = Convert.ToInt32(ds.Tables[0].Rows[0]["NumDanWeiTabID"].ToString(), 10);
            }
            bool bIsDisCount = false;
            ds = m_bllBGCKHTTab.GetAllBGPackingID(this.comboBoxContractNo.Text);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int nPackingId = Convert.ToInt32(ds.Tables[0].Rows[i]["BGPackingTabID"].ToString(), 10);
                    bIsDisCount = (bool)ds.Tables[0].Rows[i]["isDisCount"];
                    string strWhere = "GoodsMXName = '" + CurrentUGR.Cells["商品名称"].Value.ToString() + "'"
               + " and GGXHZDEtc = '" + CurrentUGR.Cells["质地"].Value.ToString() + "'"
               + " and GGXHZDEtc2 = '" + CurrentUGR.Cells["规格型号"].Value.ToString() + "'"
               + " and NumDanWeiTabID = '" + nDanweiId + "'";
                    DataSet ds2 = m_bllBGCKHTTab.FindBGPackGoodsMXTabID(strWhere, nPackingId.ToString());
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {
                            int nSJPackGoodsMXTabID = Convert.ToInt32(ds2.Tables[0].Rows[j]["BGPackGoodsMXTabID"].ToString(), 10);
                            Decimal nNum = Convert.ToDecimal(ds2.Tables[0].Rows[j]["FNumber"].ToString());
                            DGBY.Model.BGPackGoodsMXTab modelSJPackGoodsMXTabOld = m_bllBGPackGoodsMXTab.GetModel(nSJPackGoodsMXTabID);
                            modelSJPackGoodsMXTabOld.CKHTDCost = Convert.ToDecimal(CurrentUGR.Cells["报关单价"].Value.ToString());
                            modelSJPackGoodsMXTabOld.CKHTJinE = CommonCompute.ChinaRound(Convert.ToDecimal(CurrentUGR.Cells["报关单价"].Value.ToString()) * nNum, 2); 

                            //modelSJPackGoodsMXTabOld.CKHTJinE = Convert.ToDecimal(CurrentUGR.Cells["金额"].Value.ToString());
                            if (bIsDisCount)
                            {
                                modelSJPackGoodsMXTabOld.HMaoWeight = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.CKHTDCost * Frm_Main.m_dtDiscountFactor,2);
                                modelSJPackGoodsMXTabOld.HZMaoWeight = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.HMaoWeight * nNum,2);
                            }
                            else
                            {
                                //modelSJPackGoodsMXTabOld.HMaoWeight = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.CKHTDCost,2);
                                //modelSJPackGoodsMXTabOld.HZMaoWeight = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.CKHTJinE,2);
                                modelSJPackGoodsMXTabOld.HMaoWeight = modelSJPackGoodsMXTabOld.CKHTDCost;
                                modelSJPackGoodsMXTabOld.HZMaoWeight = modelSJPackGoodsMXTabOld.CKHTJinE;
                            }
                            if (Frm_Main.m_bCompleteVer == true)
                            {
                                if (dtDisPrice != modelSJPackGoodsMXTabOld.HMaoWeight)
                                {
                                    modelSJPackGoodsMXTabOld.HMaoWeight = CommonCompute.ChinaRound(dtDisPrice, 2);
                                    modelSJPackGoodsMXTabOld.HZMaoWeight = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.HMaoWeight * nNum, 2);
                                }
                            }

                            m_bllBGPackGoodsMXTab.Update(modelSJPackGoodsMXTabOld);
                        }
                    }
                }
            }

            ShowAllMXRecordInDataGridView(this.comboBoxContractNo.Text);
        }
        //private void ultraGrid2_BeforeCellUpdate(object sender, Infragistics.Win.UltraWinGrid.BeforeCellUpdateEventArgs e)
        //{
        //    ModifyDetail(e.Row.Index);
        //}

        private void ultraGrid2_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid2.ActiveCell;
                        if (null == gridCell)
                            return;     

                        int nColumnIndex = this.ultraGrid2.ActiveCell.Column.Index;
                        int nIndex = ultraGrid2.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid2.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid2.ActiveCell;
                        if (null == gridCell)
                            return;     

                        int nColumnIndex = this.ultraGrid2.ActiveCell.Column.Index;
                        int nIndex = ultraGrid2.ActiveRow.Index;
                        if (nIndex < ultraGrid2.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid2.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid2.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid2.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }

        private void ultraGrid2_BeforeRowUpdate(object sender, Infragistics.Win.UltraWinGrid.CancelableRowEventArgs e)
        {
            ModifyGoodsDetails(e.Row.Index);
            m_bHaveChange = false;
        }

        private void ultraGrid2_AfterCellUpdate(object sender, CellEventArgs e)
        {
            if (Frm_Main.m_bCompleteVer == true)
            {
                if (e.Cell.Column.Key == "金额")
                {
                    if (m_bHaveChange)
                        return;
                    int nIndex = e.Cell.Row.Index;
                    UltraGridRow CurrentUGR = ultraGrid2.Rows[nIndex];
                    Decimal dJinE = (Decimal)e.Cell.Value;
                    Decimal nFPieceNum = Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString());
                    if (nFPieceNum > 0)
                    {
                        m_bHaveChange = true;
                        CurrentUGR.Cells["单价"].Value = dJinE / nFPieceNum;

                    }
                }
                if (e.Cell.Column.Key == "单价")
                {
                    int nIndex = e.Cell.Row.Index;
                    UltraGridRow CurrentUGR = ultraGrid2.Rows[nIndex];
                    Decimal dSingleCost = (Decimal)e.Cell.Value;
                    Decimal nFPieceNum = Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString());

                    if (!m_bHaveChange)
                    {
                        if (nFPieceNum > 0)
                        {
                            CurrentUGR.Cells["金额"].Value = CommonCompute.ChinaRound(dSingleCost * nFPieceNum, 2);

                        }
                    } 

                    DataSet ds = m_bllBGCKHTTab.GetAllBGPackingID(this.comboBoxContractNo.Text);
                    bool bIsDisCount = false;
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {

                        bIsDisCount = (bool)ds.Tables[0].Rows[0]["isDisCount"];
                        if (bIsDisCount)
                        {
                            CurrentUGR.Cells["报关单价"].Value = CommonCompute.ChinaRound(dSingleCost * Frm_Main.m_dtDiscountFactor, 4);
                            CurrentUGR.Cells["报关金额"].Value = CommonCompute.ChinaRound(CommonCompute.ChinaRound(dSingleCost * Frm_Main.m_dtDiscountFactor, 2) * nFPieceNum, 2);
                        }
                        else
                        {
                            CurrentUGR.Cells["报关单价"].Value = dSingleCost;
                            CurrentUGR.Cells["报关金额"].Value = CommonCompute.ChinaRound(dSingleCost * nFPieceNum, 2);
                            //CurrentUGR.Cells["报关单价"].Value = CommonCompute.ChinaRound(dSingleCost, 4);
                            //CurrentUGR.Cells["报关金额"].Value = CommonCompute.ChinaRound(CommonCompute.ChinaRound(dSingleCost, 2) * nFPieceNum, 2);
                        }


                    }
                }
            }
            else
            {
                if (e.Cell.Column.Key == "报关金额")
                {
                    if (m_bHaveChange)
                        return;
                    int nIndex = e.Cell.Row.Index;
                    UltraGridRow CurrentUGR = ultraGrid2.Rows[nIndex];
                    Decimal dJinE = (Decimal)e.Cell.Value;
                    Decimal nFPieceNum = Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString());
                    if (nFPieceNum > 0)
                    {
                        m_bHaveChange = true;
                        CurrentUGR.Cells["报关单价"].Value = dJinE / nFPieceNum;
                        
                        //CurrentUGR.Cells["报关单价"].Value = CommonCompute.ChinaRound(dJinE / nFPieceNum, 2);
                       
                    }
                }
                if (e.Cell.Column.Key == "报关单价")
                {
                    if (m_bHaveChange)
                        return;
                    int nIndex = e.Cell.Row.Index;
                    UltraGridRow CurrentUGR = ultraGrid2.Rows[nIndex];
                    Decimal dSingleCost = (Decimal)e.Cell.Value;
                    Decimal nFPieceNum = Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString());
                    
                    if (nFPieceNum > 0)
                    {
                        
                        //CurrentUGR.Cells["报关金额"].Value = CommonCompute.ChinaRound(dSingleCost * nFPieceNum, 2);
                        CurrentUGR.Cells["报关金额"].Value = CommonCompute.ChinaRound(CommonCompute.ChinaRound(dSingleCost, 2) * nFPieceNum, 2);
                        
                    }

                    //DataSet ds = m_bllBGCKHTTab.GetAllBGPackingID(this.comboBoxContractNo.Text);
                    //bool bIsDisCount = false;
                    //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    //{

                    //    bIsDisCount = (bool)ds.Tables[0].Rows[0]["isDisCount"];
                    //    if (bIsDisCount)
                    //    {
                    //        if (!m_bHaveChange)
                    //        {
                    //            m_bHaveChange = true;
                    //            CurrentUGR.Cells["报关单价"].Value = CommonCompute.ChinaRound(dSingleCost * Frm_Main.m_dtDiscountFactor, 2);
                    //        }
                    //        CurrentUGR.Cells["报关金额"].Value = CommonCompute.ChinaRound(CommonCompute.ChinaRound(dSingleCost * Frm_Main.m_dtDiscountFactor, 2) * nFPieceNum, 2);
                    //    }
                    //    else
                    //    {
                    //        if (!m_bHaveChange)
                    //        {
                    //            m_bHaveChange = true;
                    //            CurrentUGR.Cells["报关单价"].Value = CommonCompute.ChinaRound(dSingleCost, 2);
                    //        }
                    //        CurrentUGR.Cells["报关金额"].Value = CommonCompute.ChinaRound(CommonCompute.ChinaRound(dSingleCost, 2) * nFPieceNum, 2);
                    //    }
                    //    //m_bHaveChange = true;

                    //}
                }
            }
           
        }
    }
}